﻿using Futajma.MigrationStudio.GenericPersistance;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml;

namespace Futajma.MigrationStudio.FileSystemPersistence
{
    public class FileSystemProjectPersistence : ProjectPersistence
    {

        public override void Save(object sendTo, System.Windows.Forms.TreeView sourceTreeView, System.Windows.Forms.TreeView targetTreeView, ProjectData projectData)
        {
            using (FileStream fs = new FileStream(sendTo as string, FileMode.Create))
            {
                SaveData(fs, sourceTreeView, targetTreeView, projectData);
            }
            SendProgressUpdateDone();
        }

        public override void Load(object readFrom, System.Windows.Forms.TreeView sourceTreeView, System.Windows.Forms.TreeView targetTreeView, ProjectData projectData)
        {
            using (Stream inStream = new FileStream(readFrom as string, FileMode.Open))
            {
                var progressWrapper  = new ProgressStreamWrapper(inStream);

                SendProgressInit(inStream.Length / 1000, "Load data file");

                progressWrapper.PositionChanged += progressWrapper_PositionChanged;

                LoadData(progressWrapper, sourceTreeView, targetTreeView, projectData);
            }
            SendProgressUpdateDone();
           
        }

        void progressWrapper_PositionChanged(object sender, long newPosition, long length)
        {
            SendProgressUpdate(newPosition / 1000);
        }
    }
}
